sub findMaxVar
{
    my $inFileName = shift;
    my $maxCol = -1;
    open(IN, $inFileName);
    while(<IN>) {
	chomp;
	@t = split " ";
	@t1  = split ":",$t[0];
	my ($i,$j);
	
	for ($i = 1; $i < @t; $i++)
	{
	    @t1  = split ":",$t[$i];
	    $col = int($t1[0]);
	    if($col > $maxCol){
		$maxCol = $col;
	    }
	}
    }
    close(IN);
    return $maxCol;
}

sub max ($$) { $_[$_[0] < $_[1]] }
sub min ($$) { $_[$_[0] > $_[1]] }

sub readBBRLine{
    my $line = shift;
    my $maxCol = shift;
    chomp($line);
    @t = split(" ",$line);
    @t1  = split ":",$t[0];
    
    my $lifeTime = $t1[0];
    my $censorInd = $t1[1];
    
    my %hVars;
 
    for ($i = 1; $i < @t; $i++)
    {
	@t1  = split ":",$t[$i];
	$col = int($t1[0]);
        $hVars{$col} = $t1[1];
    }
    return ($lifeTime, $censorInd, %hVars);
}

sub writeBBRLine(\%$$){
    my ($hVars, $lifeTime, $censorInd) = @_;
    my $line;
    $line = $line . "$lifeTime:$censorInd";
    my %temp = %{$hVars};
    for $i (sort {$a<=>$b} keys %{$hVars}){
	if($hVars->{$i} > 0){
	    $line = $line . " $i:$hVars->{$i}";
	}
    }
    return $line;
}

my $inFileName0 = "C:/Users/Mittal/Documents/Work/Code/survival-analysis/perl/NTDBNEW_Imputation_Test_HD_imputed_0.txt";
my $inFileName1 = "C:/Users/Mittal/Documents/Work/Code/survival-analysis/perl/NTDBNEW_Imputation_Test_HD_imputed_1.txt";
my $inFileName2 = "C:/Users/Mittal/Documents/Work/Code/survival-analysis/perl/NTDBNEW_Imputation_Test_HD_imputed_2.txt";
my $inFileName3 = "C:/Users/Mittal/Documents/Work/Code/survival-analysis/perl/NTDBNEW_Imputation_Test_HD_imputed_3.txt";
my $inFileName4 = "C:/Users/Mittal/Documents/Work/Code/survival-analysis/perl/NTDBNEW_Imputation_Test_HD_imputed_4.txt";
my $outFileName = '> C:/Users/Mittal/Documents/Work/Code/survival-analysis/perl/NTDBNEW_Imputation_Test_HD_imputed_avg.txt';

my $maxCol = max(max(max(max(findMaxVar($inFileName0),findMaxVar($inFileName1)),findMaxVar($inFileName2)),findMaxVar($inFileName3)),findMaxVar($inFileName4)) + 1;


open(IN0, $inFileName0); my(@lines0) = <IN0>; close(IN0);
open(IN1, $inFileName1); my(@lines1) = <IN1>; close(IN1);
open(IN2, $inFileName2); my(@lines2) = <IN2>; close(IN2);
open(IN3, $inFileName3); my(@lines3) = <IN3>; close(IN3);
open(IN4, $inFileName4); my(@lines4) = <IN4>; close(IN4);

open (OUT, $outFileName);

my $nLines = @lines0;
my ($line, $line0, $line1, $line2, $line3, $line4);
my (%hVars0,%hVars1,%hVars2,%hVars3,%hVars4);
my ($lifeTime,$censorInd);
for(my $i = 0; $i < $nLines; $i++){
    $line0 = $lines0[$i];
    $line1 = $lines1[$i];
    $line2 = $lines2[$i];
    $line3 = $lines3[$i];
    $line4 = $lines4[$i];
    ($lifeTime, $censorInd, %hVars0) = readBBRLine($line0,$maxCol);
    ($lifeTime, $censorInd, %hVars1) = readBBRLine($line1,$maxCol);
    ($lifeTime, $censorInd, %hVars2) = readBBRLine($line2,$maxCol);
    ($lifeTime, $censorInd, %hVars3) = readBBRLine($line3,$maxCol);
    ($lifeTime, $censorInd, %hVars4) = readBBRLine($line4,$maxCol);
    
    my %htemp = (%hVars0, %hVars1, %hVars2, %hVars3, %hVars4);
    my %hVars;
    foreach my $j (sort keys %htemp){
	my $temp = 0;
	if(exists($hVars0{$j})){
	    $temp = $temp + $hVars0{$j};
	}
	if(exists($hVars1{$j})){
	    $temp = $temp + $hVars1{$j};
	}
	if(exists($hVars2{$j})){
	    $temp = $temp + $hVars2{$j};
	}
	if(exists($hVars3{$j})){
	    $temp = $temp + $hVars3{$j};
	}
	if(exists($hVars4{$j})){
	    $temp = $temp + $hVars4{$j};
	}
	$hVars{$j} = $temp/5;
    }
    $line = writeBBRLine(%hVars,$lifeTime,$censorInd);
    print OUT "$line\n";
}

close(OUT);
